package contract.jvm;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.MethodNode;

/**
 * native方法
 */
public class NativeMethod {

    /**
     * 是否native方法
     */
    public static boolean isNativeMethod(MethodNode methodNode) {
        return (methodNode.access & Opcodes.ACC_NATIVE) != 0;
    }

    /**
     * 运行native方法
     */
    public static void runNativeMethod(Jvm jvm, String className, String methodName, String methodDesc) {
        Frame frame = jvm.jvmStack.lastElement();
        String methodFullName = MethodArea.methodFullName(className, methodName, methodDesc);
        if ("contract/sdk/ContractUtil.println(I)V".equals(methodFullName)) {
            println(frame);
        } else {
            throw new RuntimeException("native方法没有实现");
        }
    }


    /**
     * 实现native方法：ContractUtil.println(int i)
     */
    private static void println(Frame frame) {
        int i = (int) frame.localVariable.get(0);
        System.out.println(i);
    }

}
